"use strict";
/**
 * @ Author: Yoann Meclot. MSay2
 * @ Created on: 2020-08-30 09:25:59
 * @ Modified on: 2020-09-29 10:42:50
 *
 * @version 1.3
 */
/**
 * Copyright (c) 2020 MSay2, Yoann Meclot - @msay2/tspoet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeBlock = void 0;
const ArrayList_js_1 = require("../utils/ArrayList.js");
const StringBuilder_js_1 = require("../utils/StringBuilder.js");
const Utils_js_1 = require("../utils/Utils.js");
const CodeWriter_js_1 = require("./CodeWriter.js");
/**
 * The CodeBlock object allows to you write a coplexe code.
 *
 * @class CodeBlock
 * @version 1.2
 */
class CodeBlock {
    constructor(builder) {
        this.code = builder.code;
    }
    /**
     * Add a new code block.
     *
     * @type {Builder}
     * @since 1.0
     */
    static new() {
        return new Builder();
    }
}
exports.CodeBlock = CodeBlock;
/**
 * @version 1.2
 */
class Builder {
    constructor() {
        this.code = new ArrayList_js_1.ArrayList();
    }
    /**
     * Add a code line.
     *
     * @param arg The code line to be added.
     * @param values The values to be cast with the 3 expressions.
     * @example $L, $S, $O
     * @throws {NullPointerException} Check if the statement is null.
     * @throws {NullPointerException} Check if the values is null.
     * @type {Builder}
     * @since 1.0
     */
    addStatement(arg, ...values) {
        let cloneArg = Utils_js_1.Utils.checkNotNull(arg, "CodeBlock must have a non-null argument.");
        Utils_js_1.Utils.checkNotNull(values, "CodeBlock must have a non-null values.");
        let result = cloneArg;
        let length = cloneArg.length;
        let index = 0;
        for (let i = 0; i < length; i++) {
            let char = cloneArg.charAt(i);
            if (char == "$") {
                if (cloneArg.charAt(i + 1) == "S") {
                    result = result.replace("$S", `"${values[index++]}"`);
                }
                if (cloneArg.charAt(i + 1) == "L") {
                    result = result.replace("$L", `${values[index++]}`);
                }
                if (cloneArg.charAt(i + 1) == "O") {
                    result = result.replace("$O", values[index++].name);
                }
            }
        }
        let builder = new StringBuilder_js_1.StringBuilder();
        builder.append(result).append((result == "" ? "" : CodeWriter_js_1.CodeWriter.SEMICOLON));
        this.code.add(builder.toString());
        cloneArg = null;
        return this;
    }
    /**
     * Add a new code block.
     *
     * @param codeBlock The new code block.
     * @throws {NullPointerException} Check if the code block is null.
     * @type {Builder}
     * @since 1.0
     */
    addCodeBlock(codeBlock) {
        this.code.addAll(Utils_js_1.Utils.checkNotNull(codeBlock.code, "CodeBlock must have a non-null code block."));
        return this;
    }
    /**
     * Add opening brace and the code line.
     *
     * @param arg The code line to be added.
     * @param values The values to be cast with the 3 expressions.
     * @example $L, $S, $O
     * @throws {NullPointerException} Check if the values is null.
     * @type {Builder}
     * @since 1.0
     */
    beginBrace(arg, ...values) {
        Utils_js_1.Utils.checkNotNull(values, "CodeBlock must have a non-null values.");
        if (arg != undefined || arg != null) {
            let cloneArg = arg;
            let result = cloneArg;
            let length = cloneArg.length;
            let index = 0;
            for (let i = 0; i < length; i++) {
                let char = cloneArg.charAt(i);
                if (char == "$") {
                    if (cloneArg.charAt(i + 1) == "S") {
                        result = result.replace("$S", `"${values[index++]}"`);
                    }
                    if (cloneArg.charAt(i + 1) == "L") {
                        result = result.replace("$L", `${values[index++]}`);
                    }
                    if (cloneArg.charAt(i + 1) == "O") {
                        result = result.replace("$O", values[index++].name);
                    }
                }
            }
            let builder = new StringBuilder_js_1.StringBuilder();
            builder.append(result);
            this.code.add(builder.toString());
            cloneArg = null;
        }
        this.code.add(CodeWriter_js_1.CodeWriter.BEGIN_BRACE);
        return this;
    }
    /**
     * Add opening and closing braces and the code line.
     *
     * @param arg The code line to be added.
     * @param values The values to be cast with the 3 expressions.
     * @example $L, $S, $O
     * @throws {NullPointerException} Check if the values is null.
     * @type {Builder}
     * @since 1.1
     */
    nextBrace(arg, ...values) {
        this.code.add(CodeWriter_js_1.CodeWriter.END_BRACE);
        if (arg != undefined || arg != null) {
            let cloneArg = arg;
            let result = cloneArg;
            let length = cloneArg.length;
            let index = 0;
            for (let i = 0; i < length; i++) {
                let char = cloneArg.charAt(i);
                if (char == "$") {
                    if (cloneArg.charAt(i + 1) == "S") {
                        result = result.replace("$S", `"${values[index++]}"`);
                    }
                    if (cloneArg.charAt(i + 1) == "L") {
                        result = result.replace("$L", `${values[index++]}`);
                    }
                    if (cloneArg.charAt(i + 1) == "O") {
                        result = result.replace("$O", values[index++].name);
                    }
                }
            }
            let builder = new StringBuilder_js_1.StringBuilder();
            builder.append(result);
            this.code.add(builder.toString());
            cloneArg = null;
        }
        this.code.add(CodeWriter_js_1.CodeWriter.BEGIN_BRACE);
        return this;
    }
    /**
     * Add closing brace.
     *
     * @param semicolon Insert a semicolon.
     * @type {Builder}
     * @since 1.0
     */
    endBrace(semicolon) {
        this.code.add((semicolon ? CodeWriter_js_1.CodeWriter.END_BRACE_SEMICOLON : CodeWriter_js_1.CodeWriter.END_BRACE));
        return this;
    }
    /**
     * Add a new comment.
     *
     * @param arg Your comment.
     * @throws {NullPointerException} Check if the comment is null.
     * @type {Builder}
     * @since 1.2
     */
    addComment(arg) {
        this.code.add(`// ${Utils_js_1.Utils.checkNotNull(arg, "CodeBlock must have a non-null comment.")}`);
        return this;
    }
    /**
     * Build a code block.
     *
     * @type {CodeBlock}
     * @since 1.0
     */
    build() {
        return new CodeBlock(this);
    }
}
//# sourceMappingURL=CodeBlock.js.map